package jhunter.rt.conf.ext;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import jhunter.conf.apps.vo.Apps;
import jhunter.conf.module.vo.Menu;
import jhunter.conf.module.vo.MenuItem;
import jhunter.rt.conf.Node;
import jhunter.rt.conf.Reloadable;

public class AppsExt implements Reloadable {

	private Apps apps;

	private Node appsNode;

	public AppsExt(Apps apps, Node appsNode) {
		super();
		this.apps = apps;
		this.appsNode = appsNode;
	}

	public Apps getApps() {
		return apps;
	}

	public void setApps(Apps apps) {
		this.apps = apps;
	}

	public Node getAppsNode() {
		return appsNode;
	}

	public void setAppsNode(Node appsNode) {
		this.appsNode = appsNode;
	}

	/**
	 * TODO MENUS
	 * 
	 * @return
	 */
	public String getMenu() {
		List<Node> appList = this.appsNode.getChildren();// App
		Map menus = new LinkedHashMap();
		for (Node app : appList) {
			Map moduleMenu = new LinkedHashMap();
			List<Node> moduleList = app.getChildren();
			for (Node module : moduleList) {
				ModuleExt moduleExt = (ModuleExt) module.getObject();
				if(moduleExt.getModule().getMenu() != null) {
					
					Map map = new LinkedHashMap();
					String mid = moduleExt.getModule().getId();
					map.put("id", mid);
					map.put("title", mid);
					map.put("menus",  moduleExt.getMenuItem());
//					moduleMenu.put(moduleExt.getModule().getId(), moduleExt.getModule().getMenu().getMenuItem());
					moduleMenu.put(mid, map);
				}
			}
			if(!moduleMenu.isEmpty()) {
				Map map = new LinkedHashMap();
				String id = ((AppExt)app.getObject()).getApp().getId() ;
				map.put("id", id);
				map.put("title", id);
				map.put("modules", moduleMenu);
				//menus.put("title", );
//				menus.put((((AppExt) app.getObject()).getApp().getId()), moduleMenu);
				menus.put(id, map);
			}
		}
		return JSON.toJSONString(menus, false);
	}

	@Override
	public void unload() {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterLoad() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init() {
		afterLoad();
	}

}
